﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ITM495.Lab2.Core;
using ITM495.Lab2.Core.Entities;
using System.Web;

namespace ITM495.Lab2.Impl.Writers
{
    public class CsvWriter : IWriter<Tweet>
    {
        private String _lineFormat = "\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\"\r\n";

        protected String Path { get; set; }

        public CsvWriter(string path)
        {
            Path = path;
        }

        #region IWriter<Tweet> Members

        public void Write(IList<Tweet> entities)
        {
            using (var csvFile = new StreamWriter(Path, false))
            {
                foreach (var t in entities)
                {
                    csvFile.Write(_lineFormat, 
                            HttpUtility.HtmlEncode(t.Title), 
                            HttpUtility.HtmlEncode(t.Description),
                            HttpUtility.HtmlEncode(t.PubDate.ToString("d")), 
                            HttpUtility.HtmlEncode(t.Guid), 
                            HttpUtility.HtmlEncode(t.Link));
                }
            }
        }

        #endregion
    }
}
